<template>
  <div>
    <el-dialog :title="addSubCategoryForm.categoryName" :visible.sync="subDialogVisible" width="76%">
      <el-card>
        <el-row :gutter="12">
          <el-col :span="2">
            <el-button type="primary" @click="newSubCategory"
              >添加子类</el-button
            >
          </el-col>
        </el-row>
        <el-table :data="subCategoryList" border stripe>
          <el-table-column label="id" prop="id"></el-table-column>
          <el-table-column label="子类名称" prop="name"></el-table-column>
          <el-table-column label="父类名称">
               <template>{{addSubCategoryForm.categoryName}}</template>
          </el-table-column>
          <el-table-column label="填写生日信息">
            <template slot-scope="scope">
              <el-tag type="info" v-if="scope.row.needBirthday=='N'">无须</el-tag>
              <el-tag type="danger" v-if="scope.row.needBirthday=='Y'">必须</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="最晚下单时间,可次日收货" prop="lastHour"></el-table-column>
          <el-table-column label="最早送到时间" prop="deliveryStartHour"></el-table-column>
          <el-table-column label="最晚送达时间" prop="deliveryEndHour"></el-table-column>
           <el-table-column label="送达间隔分钟" prop="intervalMinute"></el-table-column>
          <el-table-column label="排序">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sortNum"
                placeholder="请输入内容"
                @change="setSortNum(scope.row.id, scope.row.sortNum)"
              ></el-input>
            </template>
          </el-table-column>
          <!-- <el-table-column label="添加时间" prop="addTime"></el-table-column> -->

          <el-table-column label="编辑">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editSubCategory(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
          <!-- 分页 -->
        </el-table>
      </el-card>
    </el-dialog>

    <el-dialog
      title="修改子类"
      :visible.sync="addSubCategoryVisible"
      width="60%"
    >
      <el-form
        :model="addSubCategoryForm"
        ref="addSubCategoryRef"
        :rules="addSubCategoryRules"
        label-width="190px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input
            v-model="addSubCategoryForm.name"
            class="form_ipt"
          ></el-input>
        </el-form-item>
        <el-form-item label="填写生日信息" prop="needBirthday">
            <el-radio-group v-model="addSubCategoryForm.needBirthday">
              <el-radio label="N">无须</el-radio>
              <el-radio label="Y">必须</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="最晚下单时间，可次日收货" prop="lastHour">
          <el-input
            v-model="addSubCategoryForm.lastHour"
            class="hhmm_ipt"
          ></el-input>
        </el-form-item>
        <el-form-item label="最早送到时间" prop="deliveryStartHour">
          <el-input
            v-model="addSubCategoryForm.deliveryStartHour"
            class="hhmm_ipt"
          ></el-input>
        </el-form-item>
        <el-form-item label="最晚送达时间" prop="deliveryEndHour">
          <el-input
            v-model="addSubCategoryForm.deliveryEndHour"
            class="hhmm_ipt"
          ></el-input>
        </el-form-item>
        <el-form-item label="送达间隔多少分钟" prop="intervalMinute">
          <el-input
            v-model="addSubCategoryForm.intervalMinute"
            class="hhmm_ipt"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addSubCategoryVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import MallApi from "@/api/Mall.js";
export default {
  props: {},
  data() {
    return {
      subDialogVisible : false,
      queryInfo: {
        categoryId: "",
      },
      subCategoryList: [],
      total: 0,
      addSubCategoryVisible: false,
      addSubCategoryForm: {
        id: "",
        name: "",
        needBirthday:'N',
        sortNum: "",
        categoryId: "",
        categoryName: "",
      },
      addSubCategoryRules: {
        subCategoryName: [
          {
            required: true,
            message: "请输入分类名字",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {},
  methods: {
    showSubCategoryList(categoryId) {
      this.subDialogVisible = true;
      this.queryInfo.categoryId = categoryId;
      this.addSubCategoryForm.categoryId = categoryId;
      MallApi.getSubCategoryList(this.queryInfo).then((res) => {
        if (res.success) {
          this.subCategoryList = res.data.subCategoryList;
          this.categoryId = res.data.category.id;
          this.categoryName = res.data.category.name;
          this.addSubCategoryForm.categoryName = res.data.category.name;
        } else {
          return this.$message.error("获取分类列表失败");
        }
      });
    },
    newSubCategory() {
      this.addSubCategoryVisible = true;
      this.addSubCategoryForm = {
        id: "",
        name: "",
        categoryId: this.queryInfo.categoryId
      };
    },
    editSubCategory(info) {
      MallApi.getSubCategoryDetail({
        id: info.id,
      }).then((res) => {
        if (res.success) {
          this.addSubCategoryVisible = true;
          this.addSubCategoryForm = JSON.parse(JSON.stringify(res.data));
        }
      });
    },

    // 添加
    addSubCategory() {
      this.$refs.addSubCategoryRef.validate((valid) => {
        if (valid) {
          MallApi.saveSubCategory(this.addSubCategoryForm).then((res) => {
            if (res.success) {
              this.addSubCategoryVisible = false;
              this.showSubCategoryList(this.queryInfo.categoryId);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    setSortNum(id, sortNum) {
      MallApi.setSubCategorySortNum({
        id: id,
        sortNum: sortNum,
      }).then((res) => {
        if (res.success) {
          this.getSubCategoryList();
        }
      });
    },
  },
};
</script>

<style>
.form_ipt {
  width: 450px;
}
.hhmm_ipt {
  width: 80px;
}
</style>
